package com.hzncc.scaffold.controller.Rear.teachingClassroom;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.Certificate;
import com.hzncc.scaffold.entity.Other.Course;
import com.hzncc.scaffold.entity.Other.Paperrecord;
import com.hzncc.scaffold.entity.Other.Userjoinedcourse;
import com.hzncc.scaffold.entity.Vo.CourseDataDetailVo;
import com.hzncc.scaffold.entity.Vo.CourseDataListVo;
import com.hzncc.scaffold.entity.Vo.StatisticVo;
import com.hzncc.scaffold.mapper.CourseMapper;
import com.hzncc.scaffold.service.*;
import com.hzncc.scaffold.utils.NumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: DrunkBear
 * @Date: 2022-05-16 14:15
 * @Description: null
 */
@RestController
@Api(tags = "教学课堂管理-课程统计")
@RequestMapping("/manage/course")
public class CourseStatisticsController {
    @Autowired
    CourseService courseService;

    @Autowired
    MajorService majorService;

    @Autowired
    UserjoinedcourseService userjoinedcourseService;

    @Autowired
    CertificateService certificateService;
    @Autowired
    PaperrecordService paperrecordService;

    @Autowired
    CoursemajorService coursemajorService;

    @Autowired
    CourseMapper courseMapper;

//    @GetMapping("/majorList")
//    @ApiOperation("专业列表")
//    public ResultT majorList() {
//        ArrayList<MajorListVo> majorListVos = new ArrayList<>();
//
//        LambdaQueryWrapper<Major> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Major::getPid, 0);
//        List<Major> majorList = majorService.list(lqw);
//
//        for (Major major : majorList) {
//            MajorListVo majorListVo = new MajorListVo();
//            BeanUtil.copyProperties(major, majorListVo);
//
//            LambdaQueryWrapper<Major> lqww = new LambdaQueryWrapper<>();
//            lqww.eq(Major::getPid, major.getId());
//            List<Major> children = majorService.list(lqww);
//
//            majorListVo.setChildren(children);
//            majorListVos.add(majorListVo);
//        }
//        return ResultT.success(majorListVos);
//    }

    @GetMapping("/courseDataList")
    @ApiOperation("课程统计列表")
    public ResultT courseDataList(Integer pageNum,Integer page, Integer pageSize, String keyword, String startTime, String endTime, Integer majorId) {
        Integer pageData = NumberUtils.pageNum(page, pageNum);
        List<CourseDataListVo> courseDataListVos = courseService.courseDataList(pageData, pageSize, keyword, startTime, endTime, majorId);
        return ResultT.success(courseDataListVos);
    }


    @GetMapping("/allCourse")
    @ApiOperation("获取统计详情")
    public ResultT allCourse() {
        //获取课程总数
        StatisticVo statisticVo = new StatisticVo();
        LambdaQueryWrapper<Course> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Course::getIsDelete, 0);
        lqw.eq(Course::getStatus, 1);
        int courseNum = courseService.count(lqw);
        statisticVo.setCourseNum(courseNum);
        //获取考核人数
        LambdaQueryWrapper<Paperrecord> lqws = new LambdaQueryWrapper<>();
        lqws.eq(Paperrecord::getClassId, null);
        int recordNum = paperrecordService.count(lqws);
        statisticVo.setRecordNum(recordNum);
        //学习人数
        int studentNum = userjoinedcourseService.count();
        statisticVo.setStudentNum(studentNum);
        //获取证书人数
        LambdaQueryWrapper<Certificate> lqwc = new LambdaQueryWrapper<>();
        lqwc.eq(Certificate::getBelongTo, 0);
        int certNum =certificateService.count(lqwc);
        statisticVo.setCertNum(certNum);
        //todo

        return ResultT.success(statisticVo);
    }

    @GetMapping("/courseDataDetail")
    @ApiOperation("课程统计详细信息")
    public ResultT courseDataDetail(Integer pageNum, Integer pageSize, String keyword, Integer courseId) {
        QueryWrapper<CourseDataDetailVo> qw = new QueryWrapper<>();
        qw.orderByDesc("id");
        if (courseId != null) {
            qw.eq("`userjoinedcourse`.`courseId`", courseId);
        }
        if (StrUtil.isNotEmpty(keyword)) {
            qw.like("`user`.`realname`", keyword);
        }
        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<CourseDataDetailVo> courseDataDetailVoList = courseMapper.detail(qw);
        return ResultT.success(courseDataDetailVoList);
    }

    @GetMapping("/exportCourseData")
    @ApiOperation("导出课程数据")
    public void exportCourseData(HttpServletResponse response) throws IOException {
        List<CourseDataListVo> courseDataListVos = courseService.courseDataList(null, null, null, null, null, null);
        ArrayList<Map<String, Object>> exportCourseDataVos = new ArrayList<>();

        for (CourseDataListVo courseDataListVo : courseDataListVos) {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();

            map.put("课程名称", courseDataListVo.getTitle());

            if (CollUtil.isNotEmpty(courseDataListVo.getMajors())) {
                if (courseDataListVo.getMajors().get(0) != null) {
                    map.put("专业方向", courseDataListVo.getMajors().get(0).getName());
                }
                if (courseDataListVo.getMajors().size() >= 2 && courseDataListVo.getMajors().get(1) != null) {
                    map.put("教学模块", courseDataListVo.getMajors().get(1).getName());
                }

            }

            map.put("学习人数", courseDataListVo.getJoinedNum());
            map.put("考核人数", courseDataListVo.getRecordNum());
            map.put("考核通过人数", courseDataListVo.getPassedNum());
            map.put("考核不通过人数", courseDataListVo.getUnpassedNum());
            map.put("获得证书人数", courseDataListVo.getCertNum());

            exportCourseDataVos.add(map);
        }

        if (CollUtil.isNotEmpty(exportCourseDataVos)) {

            ExcelWriter writer = ExcelUtil.getWriter();
            writer.write(exportCourseDataVos, true);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            String fileName = URLEncoder.encode("课程统计", "UTF-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName+".xls");
            ServletOutputStream out = response.getOutputStream();
            writer.flush(out);
            writer.close();
        }

    }
}
